package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
* @author Administrator
* @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
* @createDate 2024-06-18 16:33:41
*/
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
    implements ApartmentInfoService{

    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    ApartmentFacilityService apartmentFacilityService;
    @Autowired
    ApartmentLabelService apartmentLabelService;
    @Autowired
    ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    GraphInfoService graphInfoService;
    @Autowired
    ProvinceInfoService provinceInfoService;
    @Autowired
    CityInfoService cityInfoService;
    @Autowired
    DistrictInfoService districtInfoService;
    @Autowired
    FacilityInfoService facilityInfoService;
    @Autowired
    LabelInfoService labelInfoService;
    @Autowired
    FeeValueService feeValueService;
    @Autowired
    RoomInfoService roomInfoService;

    //分页查询
    @Override
    public IPage<ApartmentItemVo> pageItem(IPage<ApartmentItemVo> iPage, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageItem(iPage,queryVo);
    }

    //保存或更新公寓信息
    @Override
    public void saveOrUpdateInfo(ApartmentSubmitVo apartmentSubmitVo) {

        //查询省市区的信息
        ProvinceInfo provinceInfo = provinceInfoService.getById(apartmentSubmitVo.getProvinceId());
        CityInfo cityInfo = cityInfoService.getById(apartmentSubmitVo.getCityId());
        DistrictInfo districtInfo = districtInfoService.getById(apartmentSubmitVo.getDistrictId());

        apartmentSubmitVo.setProvinceName(provinceInfo.getName());
        apartmentSubmitVo.setCityName(cityInfo.getName());
        apartmentSubmitVo.setDistrictName(districtInfo.getName());

        //获取id来判断是不是修改行为
        Long apartmentId = apartmentSubmitVo.getId();
        boolean flag=false;
        if(apartmentId!=null){
            flag=true;
        }



        //保存或更新公寓信息
        saveOrUpdate(apartmentSubmitVo);

        if(flag){  //是修改,先删除公寓关联的其他外键表数据
            //删除标签关系表的数据
            apartmentLabelService.remove
                    (new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId,apartmentId));

            //删除配套关系表的数据
            apartmentFacilityService.remove
                    (new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId,apartmentId));

            //删除图片关系表的数据
            graphInfoService.remove
                    (new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,apartmentId).eq(GraphInfo::getItemType,ItemType.APARTMENT));

            //删除杂费关系表的数据
            apartmentFeeValueService.remove
                    (new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId,apartmentId));
        }





        //添加数据

        //配套  facilityInfoIds：所有配套信息的id
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if(!CollectionUtils.isEmpty(facilityInfoIds)){
            //关系表中的所有数据
            List<ApartmentFacility> facilities=new ArrayList<>();
            for (Long facilityInfoId: facilityInfoIds) {
                ApartmentFacility apartmentFacility=ApartmentFacility.builder().apartmentId(apartmentSubmitVo.getId()).facilityId(facilityInfoId).build();
                facilities.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(facilities);
        }

        //标签
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if(!CollectionUtils.isEmpty(labelIds)){
            List<ApartmentLabel> labels=new ArrayList<>();
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel=ApartmentLabel.builder().apartmentId(apartmentSubmitVo.getId()).labelId(labelId).build();
                labels.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(labels);
        }


        //杂费
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if(!CollectionUtils.isEmpty(feeValueIds)){
            List<ApartmentFeeValue> feeValueList=new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue=ApartmentFeeValue.builder().apartmentId(apartmentSubmitVo.getId()).feeValueId(feeValueId).build();
                feeValueList.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(feeValueList);
        }

        //图标
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if(!CollectionUtils.isEmpty(graphVoList)){
            List<GraphInfo> graphInfoList=new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo=new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }
    }


    //回显
    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        ApartmentDetailVo apartmentDetailVo=new ApartmentDetailVo();

        //查询公寓的基本信息
        ApartmentInfo apartmentInfo=getById(id);


        //查询公寓的配套
        List<FacilityInfo> facilityInfoList=facilityInfoService.getFacilityInfo(id);

        //查询公寓的标签
        List<LabelInfo> labelInfoList=labelInfoService.getLabelInfo(id);

        //查询公寓的杂费
        List<FeeValueVo> feeValueVoList=feeValueService.getFeeInfo(id);

        //查询公寓的图片
        List<GraphVo> graphVoList=graphInfoService.getGraphInfo(ItemType.APARTMENT,id);

        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);

        //把 apartmentInfo中的属性值复制到apartmentDetailVo中对应的属性上
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);



        return apartmentDetailVo;
    }

    //根据id删除公寓
    @Override
    public void removeByIdInfo(Long apartmentId) {

        //如果公寓有关联的房间，该公寓不能删除
        long count = roomInfoService.count(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId, apartmentId));
        if(count>0){
            //throw new RuntimeException("不能删除");
            throw new LeaseException("公寓有关联的房间，该公寓不能删除",201);
        }

        //删除公寓的关联信息

        //删除标签关系表的数据
        apartmentLabelService.remove
                (new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId,apartmentId));

        //删除配套关系表的数据
        apartmentFacilityService.remove
                (new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId,apartmentId));

        //删除图片关系表的数据
        graphInfoService.remove
                (new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,apartmentId).eq(GraphInfo::getItemType,ItemType.APARTMENT));

        //删除杂费关系表的数据
        apartmentFeeValueService.remove
                (new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId,apartmentId));

        //删除公寓的基本信息
        super.removeById(apartmentId);
    }
}




